﻿function GetCourseList(v) {
  ajaxRead("/tools/ajax.aspx?t=getCourseList&cid=" + v, "DrawCourseList(obj);");
}

function GetCourseListWithCourseID(v) {
  ajaxRead("/tools/ajax.aspx?t=getCourseListWithCourseID&cid=" + v, "DrawCourseList(obj);");
}

function DrawCourseList(obj) {
  var res = obj.getElementsByTagName("result");
  var resContainer = document.getElementById("CourseSlt");
  resContainer.length = 0;
  resContainer.options.add(new Option("请选择课程", "-1"));
  var result = "";
  if (res[0] != null && res[0] != undefined) {
    if (res[0].childNodes.length > 1) {
      for (var i = 0; i < res[0].childNodes.length; i++) {
        if (res[0].childNodes[i].nodeName == "#text") {
          i = i + 1;
        }
        var id = res[0].childNodes[i].attributes[0].value;
        var name = res[0].childNodes[i].attributes[1].value;
        resContainer.options.add(new Option(name, id));
      }
    } else {
      if (res[0].childNodes[0] != null) {
        var id = res[0].childNodes[0].attributes[0].value;
        var name = res[0].childNodes[0].attributes[1].value;
        resContainer.options.add(new Option(name, id));
      }
    }
  }
}

function GetCourseListWithCourseIDAndSetValue(v, v2) {
  ajaxRead("/tools/ajax.aspx?t=getCourseListWithCourseID&cid=" + v, "DrawCourseListAndSetValue(obj," + v2 + ");");
}

function DrawCourseListAndSetValue(obj, v) {
  var res = obj.getElementsByTagName("result");
  var resContainer = document.getElementById("CourseSlt");
  resContainer.length = 0;
  resContainer.options.add(new Option("请选择课程", "-1"));
  var result = "";
  if (res[0] != null && res[0] != undefined) {
    if (res[0].childNodes.length > 1) {
      for (var i = 0; i < res[0].childNodes.length; i++) {
        if (res[0].childNodes[i].nodeName == "#text") {
          i = i + 1;
        }
        var id = res[0].childNodes[i].attributes[0].value;
        var name = res[0].childNodes[i].attributes[1].value;
        resContainer.options.add(new Option(name, id));
      }
    } else {
      if (res[0].childNodes[0] != null) {
        var id = res[0].childNodes[0].attributes[0].value;
        var name = res[0].childNodes[0].attributes[1].value;
        resContainer.options.add(new Option(name, id));
      }
    }
  }
  if (v != "")
    resContainer.value = v;
}

function CheckForm(act) {
  var name = document.getElementById("NameTxt").value;
  var schoolhour = document.getElementById("SchoolHourTxt").value;
  var chkcoursename = $("#chkcoursenamerst").val();

  if (name == "" || chkcoursename == "1" || (schoolhour != "" && isNaN(schoolhour)) || (act == "n" && document.getElementById("ProjectSlt").value == "-1")) {
    alert("请正确填写信息");
    return false;
  } else {
    return true;
  }
}

function CheckPForm() {
  var name = document.getElementById("NameTxt").value;
  var categoryid = document.getElementById("CategorySlt").value;
  var schoolhour = document.getElementById("SchoolHourTxt").value;

  if (name == "" || categoryid == "-1" || (schoolhour != "" && isNaN(schoolhour))) {
    alert("请正确填写信息");
    return false;
  } else {
    return true;
  }
}

function CheckCourseName() {
  var act = document.getElementById("act").value;
  var v = document.getElementById("NameTxt").value;
  if (v != "") {
    if (act == "n") {
      var pid = document.getElementById("ProjectSlt").value;
      if (pid != "-1") {
        ajaxRead("/tools/ajax.aspx?t=chkCourseName&v=" + escape(v) + "&pid=" + pid + "&id=-1", "GetSetRst(obj);");
      }
    } else {
      var pid = document.getElementById("pid").value;
      var id = document.getElementById("id").value;
      ajaxRead("/tools/ajax.aspx?t=chkCourseName&v=" + escape(v) + "&pid=" + pid + "&id=" + id, "GetSetRst(obj);");
    }
}
  if (arguments[0] != undefined && arguments[0] == 'createCourse') {
      getCourseListByProject();
  }
}

function GetSetRst(obj) {
  var res = obj.getElementsByTagName('result');
  var rst;
  if (res[0] != null && res[0] != undefined) {
    if (res[0].childNodes.length > 1) {
      rst = res[0].childNodes[1].nodeValue;
    } else {
      rst = res[0].firstChild.nodeValue;
    }
  }
  if (rst == "1") {
    $("#chkCourseNameRst").html("该课程名已存在，请更换");
    $("#chkcoursenamerst").val("1");
  } else {
    $("#chkCourseNameRst").html("");
    $("#chkcoursenamerst").val("0");
  }
}

function CheckPublicCourseName() {
  var act = document.getElementById("act").value;
  var v = document.getElementById("NameTxt").value;
  if (v != "") {
    if (act == "n") {
      var pid = document.getElementById("ProjectSlt").value;
      if (pid != "-1") {
        ajaxRead("/tools/ajax.aspx?t=chkInternalPublicCourseName&v=" + escape(v) + "&pid=" + pid + "&id=-1", "GetSetRst(obj);");
      }
    } else {
      var pid = document.getElementById("pid").value;
      var id = document.getElementById("id").value;
      ajaxRead("/tools/ajax.aspx?t=chkInternalPublicCourseName&v=" + escape(v) + "&pid=" + pid + "&id=" + id, "GetSetRst(obj);");
    }
  }
}